<?php
session_start();
require_once("User.php");

class logInController
{
	private $username;
	private $password;
	private $user;
	
	function __construct($user, $pass)
	{
		$this->username = $user;
		$this->password = $pass;
		$this->user = new User;
	}

	function isValidAccount()
	{
		if(strlen($this->username) >= 10 && strlen($this->password) >= 1)
			return true;
		else return false;
	}
	function redirectUser()
	{
		if(isset($_SESSION['user']) && isset($_SESSION['pos']))
		{
			if($_SESSION['pos'] == "Manager")
			{
				header("Location: managerpage.php");
			}
			else if ($_SESSION['pos'] == "Regular")
			{
				header("Location: employeepage.php");
			}
		}
		else
		{
			$_SESSION['stat'] = "login_error";
			$_SESSION['msg'] = "Invalid username and/or password";
			header("Location: index.php");
		}
	}
	function sendRequest()
	{
		if(self::isValidAccount())
		{
			$this->user->logIn($this->username, $this->password);
			self::redirectUser();
		}
		else
		{
			$_SESSION['stat'] = "login_error";
			$_SESSION['msg'] = "Invalid username and/or password";
			header("Location: index.php");
		}
			
	}
}

$login = new logInController($_REQUEST['username'],$_REQUEST['password']);
$login->sendRequest();
?>